﻿using ExamRoomAllocationManagement.Dao;
using Xmu.Stores.Models;

namespace ExamRoomAllocationManagement.Services
{
    public class ExamService : ISingletonDependency
    {
        private IServiceProvider serviceProvider;
        private Repositories repo;
        public ExamService(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
            repo = serviceProvider.CreateScope().ServiceProvider.GetRequiredService<Repositories>();
        }

        public (object exams, object msg) GetExams(int pageIndex, int pageSize)
        {
            return (repo.Exams.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(), "ok");
        }

        public (object exams, object msg) GetAllExams()
        {
            return (repo.Exams.ToList(), "ok");
        }

        public (object exam, object msg) GetExam(Guid id)
        {
            return (repo.Exams.FirstOrDefault(er => er.ExamId == id), "ok");
        }

        public (bool success, string msg) AddExam(Exam exam)
        {
            Console.WriteLine("exam in service:" + exam);
            repo.Exams.Add(exam);
            repo.DbContex.SaveChanges();
            return (true, "ok");
        }
    }
}